package com.spx.chapter05.sink;

import com.spx.chapter05.pojo.Event;
import com.spx.util.SampleDataUtil;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * create by undeRdoG on  2022-05-01  14:15
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class FileSinkTest {


    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<Event> dataSource = env.fromCollection(SampleDataUtil.getSample());

        StreamingFileSink<String> fileSink = StreamingFileSink.<String>forRowFormat(new Path("./output"), new SimpleStringEncoder<>("UTF-8"))
                // 设置滚动策略
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                // 设置文件最大为1个字节，超过则开启新文件
                                .withMaxPartSize(1L)
                                // 设置滚动时间，单位为ms
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15L))
                                // 设置最大空闲时间， 单位为ms
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5L))
                                .build()
                ).build();

        dataSource.map(Event::toString).addSink(fileSink);

        env.execute();
    }
}
